<?php
/**
 * Created by PhpStorm.
 * User: cuipengyu
 * Date: 2019/3/23
 * Time: 0:26
 */
ignore_user_abort(); // 后台运行
set_time_limit(0); // 取消脚本运行时间的超时上限

require_once('Lib/Common/common.php');
$file_config = 'Runtime/Conf/config.php';

$file_last_runapis_time =  'Runtime/Logs/runapis';
$runrecord =  'Runtime/Logs/runrecord';

$mustStop = false;
$longSleep = 600;
$shortSleep = 300;
$minSleep =5;
$runversion = '1.13';


if(!file_exists($file_last_runapis_time)) {
    $f2=fopen($file_last_runapis_time, "w");
    fclose($f2);
}else {
    $f1 = fopen($file_last_runapis_time, 'r');
    $lasttime = fread($f1, filesize($file_last_runapis_time));
    fclose($f1);
    // 如果未超时30分钟，则不允许执行
    $lastsecond = time() - $lasttime;
    if ($lasttime > 0 &&  $lastsecond < 30*60 ) {
        echo ('已经被调用且正在执行,上次运行于 '.($lastsecond/60).'分钟前，本次请求被取消。');
        return;
    }
}



try{
    while(true){

        if(!canrun()){
            break;
        }

        $conn = conndb();
        // 获得所有的影视资源列表
        $sql = 'select cj_id,cj_url from ff_cj where cj_type =1 order by cj_id';
        $arr = querydb($conn,$sql);

        $conn->close();
        
        if($arr==null){
            dolog("没有读到采集资源列表 <br>");
            sleep($longSleep);
            continue;
        }

        // 遍历所有的影视资源 $v1.cj_id ,cj_url
        foreach($arr as $k1=>$v1){

            // 读出 [cj_id , pagecount , page ]
            $rarr = read_record();
            // 找到上一次采集id的记录
            if($rarr[0]>0 && $rarr[0]>$v1['cj_id'] ){
                continue;
            }

            // 比较是当前的cj_id
            if($rarr[0]==$v1['cj_id']){
                $page = $rarr[2]!=0?$rarr[2]:1 ;
                $pagecount = $rarr[1]!=0?$rarr[1]: -1;
            }else{
                $page = 1 ;
                $pagecount = -1;
            }

            // 一页页的抓取
            while($pagecount == -1 || $page<= $pagecount ){

                loadPage($v1['cj_url'],$v1['cj_id'],$page,$pagecount);

                if(!canrun()){
                    break;
                }
                $page++;
                save_record($v1['cj_id'], $pagecount, $page);
            }

            if(!canrun()){
                break;
            }
            global $shortSleep;
            sleep($shortSleep);

        }
        if(!canrun()){
            break;
        }
        save_record(0,0,0);

    }
}finally{
    unlink($file_last_runapis_time);

}

/**
 * @param $cjurl 影视资源地址
 * @param $cjid 影视资源 数据库中存储的id
 * @param $pageindex 页索引
 * @param $pagecount 总页数
 */
function loadPage($cjurl,$cjid,$pageindex,&$pagecount){
    try {
        // 组成 24小时 以内的抓取地址
        $url1 = $cjurl . '?ac=videolist&pg=' . $pageindex . '&p=' . $pageindex . '&h=98';
        $html1 = ff_file_get_contents($url1);

        $xmltype = '';
        $vodIds = array();

        $base64url = base64_encode($cjurl);

        if($base64url==null){
            dolog("由于未知原因 base64Url == null <br>");
            return;
        }

        if (strpos($html1, '<?xml') === 0) {
            $xmltype = 'xml';
            $a = preg_match('#pagecount="(\d+?)"#i', $html1, $mc1);
            $pagecount = $mc1[1];

            $a = preg_match_all('#<id>(\d+?)</id>#i', $html1, $mc3);
            $vodIds = $mc3[1];

        } else {
            $xmltype = 'json';
            $a = preg_match('#"pagecount":"(\d+?)"#i', $html1, $mc1);
            $pagecount = $mc1[1];

            $a = preg_match_all('#"vod_id":"(\d+?)",#i', $html1, $mc3);
            $vodIds = $mc3[1];
        }

        dolog(' +++++++++++ vodids =='.json_encode($vodIds) );

        global $file_config;
        $config = @include($file_config);
        $domain = $config['site_domain'];

        $url2 = 'http://'.$domain.'/index.php?g=admin&m=cj&a=apis&action=ids';
        $data2 = array('cjid' => $cjid, 'cjtype' => '1', 'xmlurl' => $base64url, 'xmltype' => $xmltype, 'wd' => '', 'page' => '');
        foreach ($vodIds as $k3 => $v3) {

            $data2['ids[]'] = $v3;

            dolog(' ==================== cjid ='.$cjid.' ids = '.$v3.'  ');
            // 循环执行当天新增的
            $html3 = ff_file_get_contents($url2, 200, '', $data2);
            $html3 = preg_replace('#<style[\s\S]+?</style>#', '', $html3);

            $msg1 = '<p>****' . date('Y-m-d H:i:s', time()) . ' == cjid = ' . $cjid . ' -- pagecount = ' . $pagecount . ' -- pageindex=' . $pageindex . ' -- vodIndex=' . $k3 . ' -- xmltype= ' . $xmltype . '</p> ';
            if(strpos('<ul>',$html3)>0){
                $msg = str_replace('<ul>', $msg1, $html3);
            }else{
                $msg = $msg1.$html3;
            }

            dolog($msg.' <br>');

            if (!canrun()) {
                break;
            }


            // 如果代理异常，则休眠10分钟
//            if (strpos($html3, '可能是代理异常') > 0 || strpos($html3, '请求从你的 IP 发出') > 0) {
//                global $longSleep;
//                sleep($longSleep);
//                goto vv;
//            } else {
//                global $shortSleep;
//                sleep($shortSleep);
//            }

            // 如果是新增的，则休眠30秒
            if ( strpos($html3, 'proxy') > 0 ) {
                global $shortSleep;
                sleep($shortSleep);
            }else{
                global $minSleep;
                sleep($minSleep);
            }


        }
    }catch (Exception $err){
        dolog($err);
    }

}


function conndb(){
    global $file_config;
    $config = @include($file_config);
    $dbhost = $config['db_host'];
    $dbuser = $config['db_user'];
    $dbpwd = $config['db_pwd'];
    $dbname = $config['db_name'];
    $dbport = $config['db_port'];

    $conn = new mysqli($dbhost, $dbuser, $dbpwd, $dbname,$dbport);
    if ($conn->connect_error) {
        die("连接失败: " . $conn->connect_error);
    }
    return $conn;
}

function querydb($conn,$sql){
    try {
        if (!$conn || $conn->connect_error) {
            die("连接失败: " . $conn->connect_error);
        }

        $arr = array();
        $result = $conn->query($sql);
        if ($result->num_rows > 0) {
            // 输出数据
            while ($row = $result->fetch_assoc()) {
                $arr[] = $row;
            }
            return $arr;
        } else {
            return null;
        }

    }catch (Exception $err){
        dolog($err);
    }
    return null;

}

function canrun(){
    global $file_last_runapis_time;
    global $mustStop;

    if($mustStop){
       return false;
    }

    // 停止运行
    if(!file_exists($file_last_runapis_time)){
        dolog ($file_last_runapis_time.'被删除，执行停止<br>');
        $mustStop=true;
        return false;
    }

    record_last_run();

    return true;
}

// 记录最后一次运行的时间
function record_last_run(){
    global $file_last_runapis_time;
    $f2=fopen($file_last_runapis_time, "w");
    fwrite($f2,time() );
    fclose($f2);
}

function save_record($cjid,$pagecount,$pageindex){
    global $runrecord;

    $txt = $cjid.'|'.$pagecount.'|'.$pageindex;
    $f1 = fopen($runrecord,'w');
    fwrite($f1,$txt);
    fclose($f1);
}
function read_record(){
    global $runrecord;

    if(!file_exists($runrecord)){
        $txt = '0|0|0';
        $arr = explode('|',$txt);
        return $arr;
    }
    clearstatcache();

    $f1 = fopen($runrecord,'r');
    $txt = fread($f1,filesize($runrecord));
    $arr = explode('|',$txt);
    fclose($f1);
    return $arr;
}

function dolog($msg){
    global $runversion;
    $logpath =  'Runtime/Logs/runsvr.html';

    clearstatcache();
    if(filesize($logpath)> 50000||!file_exists($logpath)){
        file_put_contents($logpath,'<!doctype html><html><head><meta charset="gb2312"><meta name="viewport" content="width=device-width, initial-scale=1"></head>' );
    }

    file_put_contents($logpath,charsetToGBK($runversion.' :: '.$msg.'<br>') ,FILE_APPEND);

}

function charsetToGBK($mixed)
{
    if (is_array($mixed)) {
        foreach ($mixed as $k => $v) {
            if (is_array($v)) {
                $mixed[$k] = charsetToGBK($v);
            } else {
                $encode = mb_detect_encoding($v, array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'));
                if ($encode == 'UTF-8') {
                    $mixed[$k] = iconv('UTF-8', 'GBK', $v);
                }
            }
        }
    } else {
        $encode = mb_detect_encoding($mixed, array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'));
        if ($encode == 'UTF-8') {
            $mixed = iconv('UTF-8', 'GBK', $mixed);
        }
    }
    return $mixed;
}